package server.plugins;

import java.io.File;
import java.io.FilenameFilter;
import java.net.URL;
import java.net.URLClassLoader;

import plugin.client.ClientPlugin;

public abstract class ClientPluginManager {
	
	private static ClientPlugin [] plugInstances = null;
	private static File [] plugFiles = null;
	
	private static void loadPlugins()
	{
		if (plugInstances == null)
		{
			File [] plugins;
			File f = new File ("ClientPlugins");
			if (!f.exists())
			{
				System.err.println("ClientPlugins directory does not exist.");
				f.mkdir();
				return;
			}
			else
			{
				System.out.println("Loading ClientPlugins");
				plugins = f.listFiles(new FilenameFilter()
				{
					public boolean accept(File f, String name)
					{
						if (name.endsWith(".jar"))
							return true;
						return false;
					}
				});
				System.out.println("Loaded "+plugins.length+" plugin(s)");
			}
			//---------------------------
			URLClassLoader cLoader;
			URL [] urls = new URL[1];
			plugInstances = new ClientPlugin[plugins.length];
			plugFiles = plugins;
			for (int i = 0; i < plugins.length; i++)
			{
				try
				{
					urls[0] = plugins[i].toURL();
				}
				catch(Exception e){e.printStackTrace();}
				cLoader = new URLClassLoader(urls);
				plugInstances[i] = getPluginInstance("plugin.client.Main", cLoader);
				//System.out.println(plugInstances[i].getName());
			}
		}
	}
	
	private static ClientPlugin getPluginInstance(String className, ClassLoader cLoader)
	{
		try
		{
			//Class cl = Class.forName("asfd",false, cLoader);
			return (ClientPlugin) Class.forName(className, true, cLoader).newInstance();
		}
		catch (Exception e){
			System.err.println("Error occured when genereting instance of a plugin: "+e);
			return null;}
	}
	
	public static String [] getNames (ClientPlugin.OS os)
	{
		loadPlugins();
		int len = 0;
		for (ClientPlugin plugin : plugInstances)
			if (plugin.getOS().equals(ClientPlugin.OS.All) || plugin.getOS().equals(os))
				len++;
		String [] arr = new String [len];
		int i=0;
		for (ClientPlugin plugin : plugInstances)
			if (plugin.getOS().equals(ClientPlugin.OS.All) || plugin.getOS().equals(os))
			{
				arr[i] = plugin.getName();
				i++;
			}
		return arr;
	}
	
	public static String [] getNames_Versions(ClientPlugin.OS os)
	{
		loadPlugins();
		if (plugInstances == null) return new String [0];
		int len = 0;
		for (ClientPlugin plugin : plugInstances)
			if (plugin.getOS().equals(ClientPlugin.OS.All) || plugin.getOS().equals(os))
				len++;
		String [] arr = new String [len];
		int i=0;
		for (ClientPlugin plugin : plugInstances)
			if (plugin.getOS().equals(ClientPlugin.OS.All) || plugin.getOS().equals(os))
			{
				arr[i] = plugin.getName()+"_"+plugin.getVersion();
				i++;
			}
		return arr;
	}
	
	public static float getVersion (String pluginName)
	{
		for (ClientPlugin plugin : plugInstances)
			if (pluginName.equals(plugin.getName()))
				return plugin.getVersion();
		return -1;
	}
	
	public static File getPluginFileByName(String pluginName)
	{
		for (int i = 0; i < plugInstances.length; i++)
		{
			if (pluginName.equals(plugInstances[i].getName()))
				return plugFiles[i];
		}
		return null;
	}
	public static ClientPlugin getPluginInstanceByName(String pluginName)
	{
		for (ClientPlugin plugin : plugInstances)
			if (pluginName.equals(plugin.getName()))
				return plugin;
		return null;
	}
}
